package com.zzyl.serve.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zzyl.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.service.INursingLevelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;

/**
 * 护理等级Service业务层处理
 *
 * @author howstrong
 * @date 2024-10-04
 */
@Service
public class NursingLevelServiceImpl extends ServiceImpl<NursingLevelMapper, NursingLevel> implements INursingLevelService {
    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 为什么key的命名中间有冒号 nursingLevel:all?
     * 因为是一种层级命名的方案,多个层级可以使用多个冒号进行实现.
     */
    @Autowired
    private static final String CACHE_KEY_PREFIX = "nursingLevel:all";

    /**
     * 查询护理等级
     *
     * @param id 护理等级主键
     * @return 护理等级
     */
    @Override
    public NursingLevel selectNursingLevelById(Long id) {
        //
        return getById(id);
    }

    /**
     * 查询护理等级列表
     *
     * @param nursingLevel 护理等级
     * @return 护理等级
     */
    @Override
    public List<NursingLevel> selectNursingLevelList(NursingLevel nursingLevel) {
        return nursingLevelMapper.selectNursingLevelList(nursingLevel);
    }

    /**
     * 新增护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int insertNursingLevel(NursingLevel nursingLevel) {
        int flag = nursingLevelMapper.insertNursingLevel(nursingLevel);
        deleteCache();
        return flag;
    }

    /**
     * 删除缓存方法
     * 为啥要写这个?
     * 是因为保存缓存是在查询的时候将数据保存到redis中的,当护理等级调用增删改方法时会出现数据库中的数据改变了
     * 但是redis中的数据没有发生改变,为了保持数据的一致性当调用增删改方法修改数据时顺便清理redis中的数据,这样
     * 当用户在执行查询时修改以后的数据也会同步到redis中保证了数据的一致性.
     */
    private void deleteCache() {
        redisTemplate.delete(CACHE_KEY_PREFIX);
    }

    /**
     * 修改护理等级
     *
     * @param nursingLevel 护理等级
     * @return 结果
     */
    @Override
    public int updateNursingLevel(NursingLevel nursingLevel) {
        int flag = nursingLevelMapper.updateById(nursingLevel);
        //清理redis中的缓存
        deleteCache();
        return flag;
    }

    /**
     * 批量删除护理等级
     *
     * @param ids 需要删除的护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelByIds(Long[] ids) {
        int flag = nursingLevelMapper.deleteNursingLevelByIds(ids);
        //清理redis中的缓存
        deleteCache();
        return flag;

    }

    /**
     * 删除护理等级信息
     *
     * @param id 护理等级主键
     * @return 结果
     */
    @Override
    public int deleteNursingLevelById(Long id) {
        int flag = nursingLevelMapper.deleteNursingLevelById(id);
        //清理redis中的缓存
        deleteCache();
        return flag;
    }

    @Override
    public List<NursingLevel> findAll() {
        //1.从缓存中获取
        List<NursingLevel> list = (List<NursingLevel>) redisTemplate.opsForValue().get(CACHE_KEY_PREFIX);
        //2.判断获取的数据是否存在
        //存在直接返回
        if (list != null && !list.isEmpty()) {
            return list;
        }

        //3.不存在查询数据库
        //查询启用状态的护理等级(status=1)
        list = nursingLevelMapper.selectList(
                new LambdaQueryWrapper<NursingLevel>().eq(NursingLevel::getStatus, 1)
        );

        //4.将数据保存到redis中
        redisTemplate.opsForValue().set(CACHE_KEY_PREFIX, list);
        return list;
    }
}
